package com.one.consumer.main.ui;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.event.Subscribe;
import com.one.common.model.event.CommonEvent;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseListActivityC;
import com.one.consumer.R;
import com.one.consumer.main.model.item.OrderItem;
import com.one.consumer.main.presenter.OrderListPresenter;
import com.one.consumer.main.ui.binder.OrderBinder;
import com.one.consumer.order.model.item.OrderEmptyItem;
import com.one.consumer.order.ui.binder.OrderEmptyBinder;

import butterknife.OnClick;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2018/11/20 17:49
 * Description：搜索订单
 **/
@Route(path = RouterPath.SEARCH_ORDER)
public class SearchOrderActivity extends BaseListActivityC<OrderListPresenter> implements IListView, OrderBinder.OrderClickListener {
    private EditText etSearch;

    @Override
    protected void initPresenter() {
        mPresenter = new OrderListPresenter(this, this);
    }

    @Override
    public void registerDate() {
        register(OrderItem.class, new OrderBinder(this));
        register(OrderEmptyItem.class, new OrderEmptyBinder());
    }

    @Override
    public void loadData() {

    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("搜索");
    }

    @Override
    public void initView() {
        super.initView();
        addTopView(R.layout.layout_search_top);
        etSearch = successView.findViewById(R.id.et_search);
        successView.findViewById(R.id.tv_search).setOnClickListener(v -> {
            String name = etSearch.getText().toString();
            if (StringUtils.isBlank(name)) return;
            mPresenter.searchOrder(name);
        });
    }


    @Override
    public boolean onEmpty() {
        addData(new OrderEmptyItem());
        return true;
    }


    /**
     * 取消订单
     */
    @Override
    public void cancelOrder(OrderItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定取消订单？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mPresenter.cancelOrder(item.getOrder_id()+"");
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                }).show();
    }

    /**
     * 删除订单
     *
     * @param item
     */
    @Override
    public void deleteOrder(OrderItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定删除订单？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mPresenter.deleteOrder(item.getOrder_id()+"");
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                }).show();
    }

    /**
     * 支付
     */
    @Override
    public void onPay(OrderItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("您确定已知维修报价并需要维修？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPresenter.payOrder(item.getOrder_id()+"");
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.show();
    }

    /**
     * 查看详情
     */
    @Override
    public void onDetail(OrderItem item) {
        RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id() + ""));
    }

    /**
     * 立即确认
     */
    @Override
    public void onConfirm(OrderItem item) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//        builder.setMessage("您确定已知维修报价并需要维修？");
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
                RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id() + ""));
//            }
//        });
//
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//
//            }
//        });
//
//        builder.show();
    }

    /**
     * 立即评价
     */
    @Override
    public void onEvaluation(OrderItem item) {
        // 跳转页面
        RouterManager.getInstance().go(RouterPath.ADD_COMMENT,
                new DefaultExtra(item.getOrder_id() + ""));
    }

    /**
     * 收到推送更新列表
     */
    @Subscribe
    public void onJpush(CommonEvent event) {
        if (event != null) {
            doRefresh();
        }
    }
}
